package com.company.Dynamic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class LetterCombinations {
    public List<String> letterCombinations(String digits) {
        if (digits==null||digits.equals("")) {
            return Collections.EMPTY_LIST;
        }
        char[] digitChars = digits.toCharArray();
        List<String> results = new ArrayList<>();
        combination(results,digitChars,0,"");
        System.out.println(results);
        return results;
    }

    private char[] getChars(char nc) {
        switch (nc) {
            case '2':
                return new char[]{'a', 'b', 'c'};
            case '3':
                return new char[]{'d', 'e', 'f'};
            case '4':
                return new char[]{'g', 'h', 'i'};
            case '5':
                return new char[]{'j', 'k', 'l'};
            case '6':
                return new char[]{'m', 'n', 'o'};
            case '7':
                return new char[]{'p', 'q', 'r', 's'};
            case '8':
                return new char[]{'t', 'u', 'v'};
            case '9':
                return new char[]{'w', 'x', 'y', 'z'};
            default:
                return null;
        }
    }

    private void combination(List<String> resultCollection,char[] digits,int cp,String curStr) {
        if (cp>=digits.length) {
            resultCollection.add(curStr);
            return;
        }

        char curDigit = digits[cp];
        char[] chars = getChars(curDigit);

        if (chars == null) {
            return;
        }
        for (char c : chars) {
            String combination = curStr + c;
            combination(resultCollection,digits,cp+1,combination);
        }
    }


}

















